﻿using System;
using System.Windows.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DDU.DataModel;
using System.Linq;

namespace DDU.ViewModel
{
    public class BooksGroupViewModel : MainViewModel
    {
        public ObservableCollection<BookInfo> ItemsOfBookGroup
        {
            get
            {
                if (ItemsOfSemesters == null)
                {
                    return null;
                }
                return new ObservableCollection<BookInfo>(GetGroupBooks());
            }
        }

        private IEnumerable<BookInfo> GetGroupBooks()
        {
            foreach (SemesterInfo semester in ItemsOfSemesters)
            {
                BookInfo book = Database.CollectionOfBooks.FirstOrDefault(b => b.SemesterGuid == semester.Guid);
                if (book != null)
                {
                    book.Semester = semester;
                    yield return book;
                }
            }
        }
    }
}
